import requests, lxml.html, csv

etree = lxml.html.etree                 # 为了使用xpath
url = 'https://movie.douban.com/top250?start={}&filter='               # 目标站点url, 待完整....


def start(url, w):
    print('正在抓取: ' + url)
    response = requests.get(url)
    response.encoding = 'utf-8'
    html = etree.HTML(response.text)
    hrefs = html.xpath('//div[@class="hd"]/a/@href')                    # 电影url链接
    movies_name = html.xpath('//div[@class="hd"]/a/span[1]/text()')     # 电影名称
    score = html.xpath('//div[@class="bd"]//span[2]/text()')            # 电影评分
    for movie_name, movie_url, movie_score in zip(movies_name, hrefs, score):
        print('正在保存' + movie_name)
        w.writerow({'电影名称': movie_name, '电影地址': movie_url, '电影评分': movie_score})
    print('保存成功')


if __name__ == '__main__':
    with open(r'C:\Users\thomas\Desktop' + '\\豆瓣排名.csv', 'a', encoding='utf-8') as f:
        w = csv.DictWriter(f, fieldnames=['电影名称', '电影地址', '电影评分'])
        w.writeheader()
        for i in range(30):                                 # 前十页
            start(url.format(i * 25), w)                    # 使用format获取完整url
